﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PSDClient
{
    public partial class DiskInfo : UserControl
    {
        public DiskInfo()
        {
            InitializeComponent();

            Downloading = false;
        }

        private string volumeName = "";
        private string diskDriver = "";
        private double freeSpace = 0;
        private double sumSpace = 0;
        private int storageType = 0;
        private bool downloading = false;

        public PSDClient DeviceInfo { get; set; }

        /// <summary>
        /// 磁盘分区名称
        /// </summary>
        public string VolumeName
        {
            get { return volumeName; }
            set
            {
                volumeName = value;
                lblDiskLabel.Text = volumeName + "(" + diskDriver + ")";
            }
        }

        /// <summary>
        /// 磁盘分区盘符
        /// </summary>
        public string DiskDriver
        {
            get
            {
                return diskDriver;
            }
            set
            {
                diskDriver = value;
                lblDiskLabel.Text = volumeName + "(" + diskDriver + ")";

                if (DeviceInfo != null && DeviceInfo.Device != null)
                {
                    if (DeviceInfo.Device.main_storage == diskDriver)
                    {
                        StorageType = 1;
                    }
                    else if (DeviceInfo.Device.second_storage == diskDriver)
                    {
                        StorageType = 2;
                    }
                    else
                    {
                        StorageType = 0;
                    }
                }
                else
                {
                    StorageType = 0;
                }
            }
        }

        /// <summary>
        /// 磁盘分区可用容量
        /// </summary>
        public double FreeSpace
        {
            get
            {
                return freeSpace;
            }
            set
            {
                freeSpace = value;

                lblDiskSpace.Text = GetSpaceText(freeSpace) + "可用，共" + GetSpaceText(sumSpace);
            }
        }

        /// <summary>
        /// 磁盘分区总容量
        /// </summary>
        public double SumSpace
        {
            get
            {
                return sumSpace;
            }
            set
            {
                sumSpace = value;

                lblDiskSpace.Text = GetSpaceText(freeSpace) + "可用，共" + GetSpaceText(sumSpace);
            }
        }

        public int StorageType
        {
            get
            {
                return storageType;
            }
            set
            {
                storageType = value;
                switch (storageType)
                {
                    case 0:
                        picStorageType.Image = Properties.Resources.Drive_Generic_0;
                        break;
                    case 1:
                        picStorageType.Image = Properties.Resources.Drive_Generic_1;
                        break;
                    case 2:
                        picStorageType.Image = Properties.Resources.Drive_Generic_2;
                        break;
                    default:
                        picStorageType.Image = Properties.Resources.Drive_Generic_0;
                        break;
                }
            }
        }

        public bool Downloading
        {
            get
            {
                return downloading;
            }
            set
            {
                downloading = value;
                this.lblDownloading.Visible = value;
            }
        }



        private string GetSpaceText(double space)
        {
            string text = "";

            string[] unit = new string[] { "B", "KB", "MB", "GB", "TB", "PB" };

            for (int i = 1; i <= unit.Length; i++)
            {
                if (space / Math.Pow(1024, i) < 1)
                {
                    text = Math.Round(space / Math.Pow(1024, i - 1), 2).ToString() + unit[i - 1];
                    break;
                }
            }

            return text;

            //if (space / 1024 < 1)
            //{
            //    return space.ToString() + "B";
            //}
            //else if (space / 1024 / 1024 < 1)
            //{
            //    return Math.Round((space / 1024), 0).ToString() + "KB";
            //}
            //else if (space / 1024 / 1024 / 1024 < 1)
            //{
            //    return Math.Round((space / 1024 / 1024), 2).ToString() + "MB";
            //}
            //else if (space / 1024 / 1024 / 1024 / 1024 < 1)
            //{
            //    return Math.Round((space / 1024 / 1024 / 1024), 2).ToString() + "GB";
            //}
            //else
            //{
            //    return Math.Round((space / 1024 / 1024 / 1024 / 1024), 2).ToString() + "TB";
            //}
        }

        private void miSetMainStorage_Click(object sender, EventArgs e)
        {
            if (DeviceInfo.Device != null)
            {
                DeviceInfo.SetMainStorage(this);
            }
        }

        private void miSetSecondaryStorage_Click(object sender, EventArgs e)
        {
            if (DeviceInfo.Device != null)
            {
                DeviceInfo.SetSecondaryStorage(this);
            }
        }

        private void miCancel_Click(object sender, EventArgs e)
        {
            if (DeviceInfo.Device != null)
            {
                DeviceInfo.SetNormalStorage(this);
            }
        }
    }

    public enum DownloadStorageType : int
    {
        normal = 0,
        main = 1,
        secondary = 2
    }
}
